package com.javalong.affairs.base.utils;

import java.io.File;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.os.Environment;

import com.javalong.affairs.base.model.User;

public class StringUtils {
    public static final int SMSCODE_LENGTH = 6;

    public static final int PWD_MIN_LENGTH = 6;

    public static final int PWD_MAX_LENGTH = 12;

    public static final String FILE_URI_PREFX = "file://";

    private static final File FILE_PREFIX = Environment
            .getExternalStorageDirectory().getAbsoluteFile();
    private static final String APP_PREFIX = "affairs";

    /**
     * @return
     * @author ygh 2015/4/27
     */
    public static boolean isMobileNO(String mobiles) {
        Pattern p = Pattern.compile("^(1[3578])[0-9]{9}$");
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }

    /**
     * @author ygh 2015/4/28
     */
    public static boolean isSmsCode(String smsCode) {
        if (smsCode != null && smsCode.length() == SMSCODE_LENGTH) {
            return true;
        }
        return false;
    }

    /**
     * @author ygh 2015/4/28
     */
    public static boolean verifyPwd(String pwd) {
        if (pwd != null && pwd.length() >= PWD_MIN_LENGTH) {
            return true;
        }
        return false;
    }

    /**
     * @author ygh 2015/4/28
     */
    public static boolean isEmpty(String str) {
        if (str == null || "".equals(str)) {
            return true;
        }
        return false;
    }

    public static String getSplitString(String str, String split, int index) {
        String[] strArr = str.split(split);
        if (index < 0) {
            if (strArr.length + index < 0) {
                return "";
            } else {
                return strArr[strArr.length + index];
            }
        } else {
            return strArr[index];
        }
    }

    public static String removeFilePrefix(String path) {
        if (!isEmpty(path) && path.startsWith(FILE_URI_PREFX)) {
            return path.substring(FILE_URI_PREFX.length());
        }
        return null;
    }

    /**
     */
    public static String changeImageParam(String url, String param) {
        if (url != null) {
            if (url.contains("?")) {
                StringBuilder sb = new StringBuilder(url.substring(0,
                        url.indexOf("?")));
                return sb.append(param).toString();
            }
            return url + param;
        } else {
            return null;
        }
    }

    /**
     *
     */
    public static String changeNum(String str, int flag) {
        String strs[] = str.split(" ");
        String text = Integer.parseInt(strs[1]) + (flag == 1 ? 1 : -1) + "";
        return strs[0] + " " + text;
    }

    /**
     * 鐎涙顑佹稉锟?閸樼粯甯€閺佹澘鐡?閸旗姳绗傞崣鍌涙殶
     */
    public static String changeText(String str, String num) {
        String strs[] = str.split(" ");
        return strs[0] + " " + num;
    }

    /**
     *
     */
    public static String changeText(String str, long num) {
        String strs[] = str.split(" ");
        return strs[0] + " " + num;
    }

    /**
     */
    public static int getNum(String str) {
        String strs[] = str.split(" ");
        return Integer.parseInt(strs[1]);
    }

    public static String trim(String name) {
        return name.trim();
    }

    public static String getRandomFilePath() {
        String uid = UUID.randomUUID().toString();
        if (FILE_PREFIX.isDirectory()) {
            return FILE_PREFIX + "/" + APP_PREFIX + "/" + uid;
        } else {
            return "/" + APP_PREFIX + "/" + uid;
        }
    }

    public static String join(String joinStr, Collection<String> values) {
        StringBuilder sb = new StringBuilder();
        for (String value : values) {
            sb.append(value).append(joinStr);
        }
        int startIndex = sb.lastIndexOf(joinStr);
        if (startIndex >= 0) {
            sb.delete(startIndex, sb.length());
        }
        return sb.toString();
    }

    public static String getMessageType(int type) {
        if (type == 1) {
            return "通知";
        } else {
            return "文件";
        }
    }

    public static String getUserIds(List<User> userList) {
        StringBuilder userStr = new StringBuilder();
        for (User u : userList) {
            userStr.append(u.id).append(",");
        }
        if (userStr.toString().endsWith(",")) {
            userStr.deleteCharAt(userStr.length() - 1);
        }
        return userStr.toString();
    }
}
